package com.hankol.winecellar

import android.Manifest
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseActivity
import com.baselibrary.extension.checkValue
import com.baselibrary.extension.toast
import com.baselibrary.fragment.CommonDynamicFragment
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.permission.PermissionCollection
import com.baselibrary.permission.PermissionsUtils
import com.baselibrary.permission.callback.PermissionsCall
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.AppManager
import com.baselibrary.utils.DoubleClickHelper
import com.baselibrary.utils.EventBusUtils
import com.baselibrary.utils.SpUtils
import com.hankol.winecellar.arouter.WineryHelp
import com.hankol.winecellar.databinding.ActivityWineryDynamicManageBinding
import com.hankol.winecellar.utils.BaseConstant
import com.hankol.winecellar.viewmodel.WineCellarViewModel
import com.media.revision.RevisionMediaActivity
import com.xinlan.imageeditlibrary.EditImgMainActivity
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**酒窖动态管理
 * @author: merbng
 * @description:
 */
@Route(path = ActivityRouter.WINE_CELLAR.WINCE_DYNAMIC_MANAGER)
class WineryDynamicManageActivity : BaseActivity(), View.OnClickListener {

    private lateinit var viewModel: WineCellarViewModel
    private lateinit var binding: ActivityWineryDynamicManageBinding

    override fun getLayoutId(): Int {
        return R.layout.activity_winery_dynamic_manage
    }

    override fun init() {
        setStatusBarHeight(R.color.color_FFFFFF)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        binding = getDataBinding()
        viewModel = getActivityViewModelProvider(this).get(WineCellarViewModel::class.java)
//        if (intent.extras.checkNotNull()) {
//            if (ARouterUtils.parseParams(intent.extras!!, "wineryCode").size > 0) {
//                wineryCode = ARouterUtils.parseParams(intent.extras!!, "wineryCode")[0]
//            }
//        }
        initView()
        initListener()
        initObserver()

        SensorsDataManager.instance.setWineryName(SpUtils.getInstance().currentWineryInfo.wineryName.checkValue())
    }

    private fun initListener() {
        binding.imgBack.setOnClickListener(this)
        binding.imgAdd.setOnClickListener(this)
    }

    private fun initView() {
        if (SpUtils.getInstance().currentWineryInfo.wineryCode.equals("0")) {
            //没有发布过酒窖 显示占位
            binding.llEmpty.visibility = View.VISIBLE
            return
        }
        supportFragmentManager.beginTransaction().add(R.id.fl_container, CommonDynamicFragment.newInstance(CommonDynamicFragment.DYNAMIC_TYPE_WINERY_MANAGER, "${SpUtils.getInstance().userId}")).commit()
    }

    private fun initObserver() {

    }


    override fun onClick(v: View) {
        when (v.id) {
            R.id.img_back -> {
                finish()
            }
            R.id.img_add -> {
                if (!DoubleClickHelper.isOnDoubleClick()) {
                    PermissionsUtils.getInstance().chekPermissions(this, PermissionCollection.CUSTOM.addPermissionList(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA
                    ), object : PermissionsCall() {
                        override fun permissionsPass() {
                            if (!SpUtils.getInstance().isLogin) {
                                AppManager.getAppManager().openLoginPage()
                                return
                            }

                            EventBusUtils.postSticky(BaseConstant.loadPublishWinery(SpUtils.getInstance().currentWineryInfo.wineryCode))
                            /* if (helper.getPublishStatus()) {
                                 "有发布任务未完成，请稍后再试".toast()
                             } else {
                                 val intent = Intent(this@WineryDynamicManageActivity, RevisionMediaActivity::class.java)
                                 intent.putExtra("type", ImageModel.TYPE_ALL)
                                 intent.putExtra("maxstr", Constants.PUBLISH_IMG_MAX)
                                 startActivity(intent)
                            }*/
                        }
                    })
                }
            }
        }
    }

    private val mWineMealHelp = ActivityRouter.getARouterInterface<WineryHelp>(ActivityRouter.WINE_CELLAR.WINE_CELLAR_HELP)

    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN) fun onEvent(messageBus: MessageBus) {
        if (messageBus.codeType == MessageBus.msgid_notify_total_status) {
            AppManager.getAppManager().finishActivity(EditImgMainActivity::class.java)
            mWineMealHelp.finishVideoEditActivity()
            AppManager.getAppManager().finishActivity(RevisionMediaActivity::class.java)
        }
    }

    override fun onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        super.onDestroy()
    }
}